package com.xlh.job;

import com.xlh.pojo.user.Class;
import com.xlh.service.statistic.StatisticService;
import com.xlh.service.user.ClassService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 统计
 * <p>
 * Created by lx on 2020/10/26.
 */
@Component
public class StatisticTask {

    private final static Logger LOGGER = LoggerFactory.getLogger(StatisticTask.class);

    @Autowired
    private ClassService classService;

    @Autowired
    private StatisticService statisticService;

    @Scheduled(cron = "0 0 3 * * *")
    public void generateCode() {

        LOGGER.info("statistic task start");

        try {

            List<Class> classes = classService.listClass(null, null);
            if (CollectionUtils.isNotEmpty(classes)) {
                classes.forEach(classInfo -> {
                    try {
                        statisticService.updateStatistic(classInfo.getId());
                    } catch (Exception e) {
                        LOGGER.error("statistic task error", e);
                    }
                });
            }
        } catch (Exception e) {
            LOGGER.error("statistic task error", e);
        }

        LOGGER.info("statistic task end");

    }

}
